package com.edu.edu.week_project01.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.edu.edu.week_project01.R;
import com.edu.edu.week_project01.activity.Configs;
import com.edu.edu.week_project01.activity.adapter.ProgrameDetailAdapter;
import com.edu.edu.week_project01.activity.bean.ProgrameBean;
import com.edu.edu.week_project01.activity.bean.ProgrameDetailBean;
import com.edu.edu.week_project01.activity.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ProgrameDetailActivity extends AppCompatActivity {

    private final int LOAD_SUCCEDD = 0;
    private final int LOAD_FAILED = 1;
    private GridView mGv;
    private List<ProgrameDetailBean.DataBean> list = new ArrayList<>();
    private List<ProgrameDetailBean.DataBean> myData;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case LOAD_SUCCEDD:
                     list.addAll(0,(List<ProgrameDetailBean.DataBean>) msg.obj) ;
                    adapter.notifyDataSetChanged();//刷新数据
                    //点击子栏目跳转到该直播详情详情页面
                    dropProgrameDetail();
                    break;
                case LOAD_FAILED:
                    Toast.makeText(ProgrameDetailActivity.this,"网络请求失败",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    private ProgrameDetailAdapter adapter;
    private String slug;

    //点击子栏目跳转到该栏目直播详情页面
    private void dropProgrameDetail() {
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                ProgrameDetailBean.DataBean bean = (ProgrameDetailBean.DataBean) adapter.getItem(position);
                //跳转到详情页面
                String uid = bean.getUid();
                Intent intent = new Intent(ProgrameDetailActivity.this,ProgrameLiveDetailActivity.class);
                intent.putExtra("detail",uid);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_programe_detail);
        //初始化刷新控件
        initView();
        //下载解析json
        LoadJson();
        adapter = new ProgrameDetailAdapter(list);
        mGv.setAdapter(adapter);

    }
    private void LoadJson() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String str = getSystemTime.getTime(Configs.JSON_URL_PROGRAME_DETAIL);
                    String url = str.replace("#d",slug);
                    String data = OkHttpUtils.getJsonFromUrl(url);
                    JSONObject obj = new JSONObject(data);
                    JSONArray array = obj.getJSONArray("data");
                    Gson gson=new Gson();
                    myData = gson.fromJson(array.toString(),new TypeToken<List<ProgrameDetailBean.DataBean>>() {}.getType());
                    Message meg = Message.obtain();
                    if(myData.size() > 0){
                        meg.what=LOAD_SUCCEDD;
                        meg.obj = myData;
                    }else {
                        meg.what=LOAD_FAILED;
                    }
                    mHandler.sendMessage(meg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void initView() {
        final PullToRefreshGridView mPull = (PullToRefreshGridView)findViewById(R.id.programe_deail_refresh);
        //获取slug值
        Intent intent = getIntent();
        ProgrameBean bean = (ProgrameBean) intent.getSerializableExtra("slug");
        //获取栏目类型
        slug = bean.getSlug();
        //拼接接口
        mPull.setScrollingWhileRefreshingEnabled(true);
        mPull.setMode(PullToRefreshBase.Mode.BOTH);
        mGv = mPull.getRefreshableView();
        mGv.setNumColumns(2);//设置为两列
        mGv.setHorizontalSpacing(6);
        mGv.setHorizontalSpacing(6);
        //刷新数据
        mPull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                LoadJson();
                mPull.onRefreshComplete();//停止刷新
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                LoadJson();
                mPull.onRefreshComplete();//停止刷新
            }
        });
    }
}
